package com.yygo.webapp.controller.money;

import com.yygo.Constants;
import com.yygo.model.LoanRequest;
import com.yygo.model.Prepayment;
import com.yygo.model.User;
import com.yygo.service.LoanRequestManager;
import com.yygo.service.MoneyService;
import com.yygo.service.PrepaymentManager;
import com.yygo.service.UserManager;
import com.yygo.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ExtendedModelMap;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.security.Principal;
import java.text.ParseException;
import java.util.Date;
import java.util.List;

/**
 * Created by tiansha on 2015/11/7.
 */
@Controller
public class PrepaymentController {

    @Autowired
    private LoanRequestManager loanRequestManager;

    @Autowired
    private MoneyService moneyService;

    @Autowired
    private UserManager userManager;

    @Autowired
    private PrepaymentManager prepaymentManager;

    @RequestMapping(value = "/prepaymentform", method = RequestMethod.GET)
    public Model showForm(HttpServletRequest request, Principal principal) {
        String id = request.getParameter("reqId");
        Model model = new ExtendedModelMap();
        LoanRequest loanRequest = loanRequestManager.loadWholeLoanRequest(new Long(id));
        Prepayment prepayment = new Prepayment();
        prepayment.setLoanRequest(loanRequest);

        model.addAttribute("prepayment", prepayment);
        return model;
    }

    @RequestMapping(value = "/submitPrepayment", method = RequestMethod.POST)
    public String submitPrepayment2(HttpServletRequest request, Principal principal) {
        String reqId = request.getParameter("loanRequest.id");
        LoanRequest loanRequest = loanRequestManager.loadWholeLoanRequest(new Long(reqId));
        User loginUser = userManager.getUserByUsername(principal.getName());

        String serialNo = request.getParameter("serialNo");
        String payDate = request.getParameter("payDate");

        Prepayment prepayment = new Prepayment();
        prepayment.setSerialNo(serialNo);
        try {
            prepayment.setPayDate(DateUtil.convertStringToDate(payDate));
        } catch (ParseException ex) {

        }

        prepayment.setStatus(Constants.STATUS_PENDING);
        prepayment.setLoanRequest(loanRequest);
        prepayment.setBondCompany(loginUser.getBondCompany());
        loanRequest.setPrepayment(prepayment);
        prepaymentManager.save(prepayment);
        return "redirect:/loanRequests";
    }

    @RequestMapping(value = "/auditPrepayment", method = RequestMethod.POST)
    public String auditPrepayment(@RequestParam(required = true, value = "paymentId") Long paymentId, @RequestParam(required = false, value = "payDate") Date payDate,
                                   @RequestParam(required = false, value = "serialNo") String serialNo, Principal principal) {
        User loginUser = userManager.getUserByUsername(principal.getName());
        moneyService.approvePrepayment(paymentId, principal.getName());

        return "redirect:/loanRequests";
    }

    @RequestMapping(value = "/prepayments", method = RequestMethod.GET)
    public Model prepayments(Principal principal) {
        User loginUser = userManager.getUserByUsername(principal.getName());
        Model model = new ExtendedModelMap();
        List<Prepayment> prepaymentList = prepaymentManager.getPendingPrepayments(loginUser);
        model.addAttribute("prepaymentList", prepaymentList);
        return model;
    }

    @RequestMapping(value="/batchAuditPrepayment", method = RequestMethod.GET)
    public String batchReceive(@RequestParam(required = true, value = "selectedIds") Long[] selectedIds, Principal principal) {
        for (Long paymentId : selectedIds) {
            moneyService.approvePrepayment(paymentId, principal.getName());
        }
        return "redirect:/prepayments";
    }

}
